﻿using Assessment.BLL;
using Assessment.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_Gauge_GaugeConfirm : BasePage_Index
{
    #region 属性
    public int MedicalRecordId
    {
        get { return Request.QueryString["MedicalRecordId"] == "" ? 0 : Convert.ToInt32(Request.QueryString["MedicalRecordId"]); }
    }
    #endregion


    T_GaugeRecordBLL bll = new T_GaugeRecordBLL();
    T_GaugeRecordItemBLL item_bll = new T_GaugeRecordItemBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            base.SetBtnGotoBackUrl("GaugeRecordEdit.aspx?Action=Add");

            BindRepeaterList();
        }
    }

    #region 绑定Repeater
    private void BindRepeaterList()
    {
        Dictionary<int, string> dicSelectedGauge = Session["dicSelectedGauge"] == null ? new Dictionary<int, string>() : (Dictionary<int, string>)Session["dicSelectedGauge"];
        this.rpt_List.DataSource = dicSelectedGauge;
        this.rpt_List.DataBind();

        this.lbl_Count.Text = dicSelectedGauge.Count.ToString();
    }
    #endregion


    #region RepeaterList操作
    protected void rpt_List_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int key = Convert.ToInt32(e.CommandArgument);
            Dictionary<int, string> dicSelectedGauge = Session["dicSelectedGauge"] == null ? new Dictionary<int, string>() : (Dictionary<int, string>)Session["dicSelectedGauge"];
            if (dicSelectedGauge.ContainsKey(key))
            {
                dicSelectedGauge.Remove(key);
            }
            Session["dicSelectedGauge"] = dicSelectedGauge;
            BindRepeaterList();
        }
    }

    protected void rpt_List_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    }
    #endregion


    #region 提交按钮
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        Dictionary<int, string> dicSelectedGauge = Session["dicSelectedGauge"] == null ? new Dictionary<int, string>() : (Dictionary<int, string>)Session["dicSelectedGauge"];
        try
        {
            T_GaugeRecord Record = new T_GaugeRecord();
            Record.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Record.CreateUserId = base.CurrentSession.UserID;
            Record.MedicalRecordId = MedicalRecordId;
            Record.Score = 0;
            Record.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Record.UpdateUserId = base.CurrentSession.UserID;
            Record.Valid = 1;

            Record.GaugeRecordId = bll.Add(Record);

            foreach (var item in dicSelectedGauge)
            {
                T_GaugeRecordItem RecordItem = new T_GaugeRecordItem();
                RecordItem.GaugeId = item.Key;
                RecordItem.GaugeRecordId = Record.GaugeRecordId;
                //Gauge.MedicalRecordGaugeId
                RecordItem.Score = 0;

                item_bll.Add(RecordItem);
            }
            //base.Alert("操作成功！");
            Response.Redirect("GaugeRecordEdit.aspx?MedicalRecordId=" + MedicalRecordId + "&GaugeRecordId=" + Record.GaugeRecordId);


        }
        catch (Exception ex)
        {
            base.Alert("操作出现异常！");
        }
    }
    #endregion
}